package com.cskaoyan.utils;

import java.util.Map;

/**
 * @author rebaogu
 * @description 对信息加密 返回加密后的数据
 * @date 2022.04.11 16:21
 * @package com.cskaoyan.util
 * @Classname Encipher2Message
 */
public class Password2Message {

    static String privateKey = null;
    static String publicKey = null;

    static {

        try {
            Map<String, Object> keyPair = RSAUtil.genKeyPair();
            privateKey = RSAUtil.getPrivateKey(keyPair);
            publicKey = RSAUtil.getPublicKey(keyPair);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 公钥加密
    public static byte[] Encipher2Message(String content) throws Exception {
        // String content = "松哥真帅";
        // 公钥加密
        byte[] encryptBytes = RSAUtil.encryptByPublicKey(content.getBytes(), publicKey);
        return encryptBytes;
    }

    // 私钥解密
    public static String Decrypt2Password(byte[] encryptBytes) throws Exception {

        byte[] resultBytes = RSAUtil.decryptByPrivateKey(encryptBytes, privateKey);
        String result = new String(resultBytes);
        return result;
    }

}
